package com.jframe.sys.orm.dao.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.jframe.sys.utils.StringUtil;

public class HibernateTemp {
	
	SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Object get(Class clz, Serializable id) {
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		try {
			Object obj = session.get(clz, id);
			tx.commit();
			session.close();
			return obj;
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
			session.close();
			return null;
		}	
	}

	public Serializable save(Object obj) {
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		try {
			Serializable id = session.save(obj);
			session.flush();			
			tx.commit();
			session.close();
			return id;
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
			session.close();
		}
		return 0;
	}
	
	public void saveOrUpdate(Object obj){
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		try {
			session.saveOrUpdate(obj);
			session.flush();			
			tx.commit();	
			session.close();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
			session.close();
		}
	}
	
	public void update(Object obj) {
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		try {
			session.update(obj);
			session.flush();
			tx.commit();	
			session.close();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
			session.close();
		}
	}
	
	public void delete(Object obj){
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		try {
			session.delete(obj);
			tx.commit();	
			session.close();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
			session.close();
		}
	}
	
	
	public void delete(String hql,String ids) {
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		try {
			Query query = session.createQuery(hql);
			String id[] = ids.split(",");
			List<Integer> list = new ArrayList<Integer>();
			for(String idtemp:id){
				list.add(StringUtil.parseInt(idtemp));
			}			
			query.setParameterList("ids", list);
			query.executeUpdate();
			tx.commit();	
			session.close();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
			session.close();
		}
	}
	
	public Serializable execute(String hql){
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		try {
			Query query = session.createQuery(hql);
			Serializable id = query.executeUpdate();
			tx.commit();	
			session.close();
			return id;
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
			session.close();
		}
		return 0;
	}
	
	public Serializable execute(String hql,Object param){
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		try {
			Query query = session.createQuery(hql);
			query.setParameter(0, param);
			Serializable id = query.executeUpdate();
			tx.commit();
			session.close();
			return id;
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
			session.close();
		}
		return 0;
	}
	
	public Serializable execute(String hql,Object[] params){
		if(params==null){
			return this.execute(hql);
		}else if(params.length==1){
			return this.execute(hql, params[0]);
		}else{
			Session session = sessionFactory.openSession();
			Transaction tx = session.beginTransaction();
			try {
				Query query = session.createQuery(hql);
				int i=0;
				for(Object obj:params){
					query.setParameter(i, obj);
					i++;
				}
				Serializable id = query.executeUpdate();
				tx.commit();	
				session.close();
				return id;
			} catch (HibernateException e) {
				e.printStackTrace();
				tx.rollback();
				session.close();
			}
			return 0;
		}
	}
		
	public List find(String hql) {
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		try {
			Query query = session.createQuery(hql);
			List ls = query.list();
			tx.commit();	
			session.close();
			return ls;
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
			session.close();
		}	
		return new ArrayList();		
	}
	
	public List find(String hql, Object param) {
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		try {
			Query query = session.createQuery(hql);
			query.setParameter(0, param);
			List ls = query.list();
			tx.commit();
			session.close();
			return ls;
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
			session.close();
		}		
		return new ArrayList();			
	}
	
	public List find(String hql, Object[] params) {
		if(params==null){
			return this.find(hql);
		}else if(params.length==1){
			return this.find(hql, params[0]);
		}else{
			Session session = sessionFactory.openSession();
			Transaction tx = session.beginTransaction();
			try {
			Query query = session.createQuery(hql);
			int i = 0;
			for (Object obj : params) {
				query.setParameter(i,obj);
				i++;
			}
			List ls = query.list();
			tx.commit();
			session.close();
			return ls;
			} catch (HibernateException e) {
				e.printStackTrace();
				tx.rollback();
				session.close();
			}		
			return new ArrayList();		
		}
	}
	
	public List find(String hql,int start,int limit){		
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		try {
			Query query = session.createQuery(hql);
			query.setFirstResult(start);
			query.setMaxResults(limit);
			List ls = query.list();
			tx.commit();	
			session.close();
			return ls;
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
			session.close();
		}		
		return new ArrayList();	

	}
	
	public List find(String hql,Object param,int start,int limit){
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		try {
			Query query = session.createQuery(hql);
			query.setParameter(0, param);
			query.setFirstResult(start);
			query.setMaxResults(limit);
			List ls = query.list();
			tx.commit();		
			session.close();
			return ls;
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
			session.close();
		}		
		return new ArrayList();	
	}
	
	public List find(String hql,Object[] params,int start,int limit){
		if(params==null){
			return this.find(hql, start, limit);
		}else if(params.length==1){
			return this.find(hql, params[0], start, limit);
		}else{
			Session session = sessionFactory.openSession();
			Transaction tx = session.beginTransaction();
			try {
				Query query = session.createQuery(hql);
				int i = 0;
				for (Object obj : params) {
					query.setParameter(i,obj);
					i++;
				}
				query.setFirstResult(start);
				query.setMaxResults(limit);
				List ls = query.list();
				tx.commit();		
				session.close();
				return ls;
			} catch (HibernateException e) {
				e.printStackTrace();
				tx.rollback();
				session.close();
			}		
			return new ArrayList();	
		}
	}
	

	public List findByIds(String hql,String ids){
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		try {
			Query query = session.createQuery(hql);
			String id[] = ids.split(",");
			List<Integer> list = new ArrayList<Integer>();
			for(String idtemp:id){
				list.add(StringUtil.parseInt(idtemp));
			}			
			query.setParameterList("ids", list);
			List ls = query.list();
			tx.commit();		
			session.close();
			return ls;
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
			session.close();
		}		
		return new ArrayList();	
	}
	
	public List findByIds(String hql,String ids,int start,int limit){
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		try {
			Query query = session.createQuery(hql).setString("ids", ids);
			query.setFirstResult(start);
			query.setMaxResults(limit);
			List ls = query.list();
			tx.commit();		
			session.close();
			return ls;
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
			session.close();
		}		
		return new ArrayList();	
	}
	
	/**
	 * 
	 * @param hql " from model"
	 * @return
	 */
	public int queryForIntByHql(String hql){
		int count = 0;
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		try {
			Query query = session.createQuery("select count(*) "+hql);
			count = ((Long) query.iterate().next()).intValue();
			tx.commit();
			session.close();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
			session.close();
		}
	
		return count;
	}
	
	/**
	 * 
	 * @param hql " from model where key=? "
	 * @param param
	 * @return
	 */
	public int queryForIntByHql(String hql,Object param){
		int count = 0;
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		try {
			Query query = session.createQuery("select count(*) "+hql);
			query.setParameter(0,param);
			count = ((Long) query.iterate().next()).intValue();
			tx.commit();
			session.close();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
			session.close();
		}
	
		return count;
	}
	
	/**
	 * 
	 * @param hql " from model where key1=? and key2=? ....."
	 * @param params
	 * @return
	 */
	public int queryForIntByHql(String hql,Object[] params){
		int count = 0;
		if(params==null){
			return this.queryForIntByHql(hql);
		}else if(params.length==1){
			return this.queryForIntByHql(hql, params[0]);
		}else{
			Session session = sessionFactory.openSession();
			Transaction tx = session.beginTransaction();
			try {
				Query query = session.createQuery("select count(*) "+hql);
				int i = 0;
				for (Object obj : params) {
					query.setParameter(i,obj);
					i++;
				}
				count = ((Long) query.iterate().next()).intValue();
				tx.commit();
				session.close();
			} catch (HibernateException e) {
				e.printStackTrace();
				tx.rollback();
				session.close();
			}
			
			return count;
		}
	}
	
	public int queryForIntByHql(String hql,String ids){
		int count = 0;
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		try {
			Query query = session.createQuery("select count(*) "+hql);
			String id[] = ids.split(",");
			List<Integer> list = new ArrayList<Integer>();
			for(String idtemp:id){
				list.add(StringUtil.parseInt(idtemp));
			}			
			query.setParameterList("ids", list);
			count = ((Long) query.iterate().next()).intValue();
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
		session.close();
		return count;
	}

}
